package com.telit.firecommand.facade;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.telit.common.config.VoiceAliServerConfig;
import com.telit.common.dto.VoiceRealFireDto;
import com.telit.common.dto.VoiceSuspectedFireDto;
import com.telit.common.util.VoiceUtils;
import com.telit.firecommand.entity.CommFireAlarm;
import com.telit.firecommand.service.FireQueryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yuzhanxiang
 * @description: 语音通知推送
 * @copyright 天立泰科技股份有限公司
 * @since 2022/4/22 17:47
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class VoicePushFacadeService {

    private final VoiceAliServerConfig voiceAliServerConfig;

    private final FireQueryService fireQueryService;

    /**
     * 语音发送方法
     *
     * @param fireId  火情id
     * @param mobiles 电话号码
     * @param status  语音模板状态码 1-真实火情模板，2-疑似火情模板,拒绝 null
     * @return 发送结果，不保证一定发送成功，不保证为阿里短信是否成功发送，只保证触发
     */
    @SuppressWarnings("all")
    public boolean pushVoice(Long fireId, List<String> mobiles, int status) {
        final Class<Object> clazz = voiceAliServerConfig.getParamClass(Convert.toStr(status));
        final String template = voiceAliServerConfig.getTemplate(Convert.toStr(status));
        if (ObjectUtil.isNull(clazz) || StrUtil.isEmpty(template)) {
            log.error("语音通知调用失败,约束火情! status[{}]", status);
            return false;
        }
        final Object objParam = ClassUtil.createInstance(clazz, Boolean.FALSE);
        //查询火情信息
        final CommFireAlarm commFireAlarm = fireQueryService.queryById(fireId);
        //临时处理 暂时不处理3
        if (status == 1) {
            final VoiceRealFireDto dto = (VoiceRealFireDto) objParam;
            dto.setAddress(commFireAlarm.getDetailAddress());
            VoiceUtils.dialNoitce(mobiles, template, dto);
        } else if (status == 2) {
            final VoiceSuspectedFireDto dto = (VoiceSuspectedFireDto) objParam;
            dto.setAddress(commFireAlarm.getDetailAddress());
            VoiceUtils.dialNoitce(mobiles, template, dto);
        }
        return true;
    }


}
